package com.lh.music.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.lh.music.R;
import com.lh.music.entity.Song;

import java.util.List;

/**
 * Created by a on 2016/12/15.
 */

public class SongDetailAdapter extends BaseAdapter {
    private Context context;
    private List<Song> mData;
    private OnMoreClickListener onMoreClickListener;
    public SongDetailAdapter(Context context, List<Song> mData) {
        this.context = context;
        this.mData = mData;
    }

    public OnMoreClickListener getOnMoreClickListener() {
        return onMoreClickListener;
    }

    public void setOnMoreClickListener(OnMoreClickListener onMoreClickListener) {
        this.onMoreClickListener = onMoreClickListener;
    }

    @Override
    public int getCount() {
        return mData.size();
    }

    @Override
    public Object getItem(int position) {
        return mData.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder=null;
        if (convertView==null){
            convertView= LayoutInflater.from(context)
                    .inflate(R.layout.adapter_danqu,null);
            holder=new ViewHolder(convertView);
            convertView.setTag(holder);
        }else{
            holder= (ViewHolder) convertView.getTag();
        }
        //控制小喇叭的显示隐藏
        if (mData.get(position).isVisiable()){
            holder.play.setVisibility(View.VISIBLE);
        }else{
            holder.play.setVisibility(View.GONE);
        }
        holder.name.setText(mData.get(position).getTitle());
        holder.artist.setText(mData.get(position).getArtist()+"-"+mData.get(position).getAlbum());
        holder.more.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onMoreClickListener!=null){
                    onMoreClickListener.onMoreClickListener(mData.get(position));
                }
            }
        });
        return convertView;
    }
    static class ViewHolder{
        ImageView play;
        TextView name;
        TextView artist;
        ImageView more;
        public ViewHolder(View itemView) {
            play= (ImageView) itemView.findViewById(R.id.play);
            more= (ImageView) itemView.findViewById(R.id.more);
            name= (TextView) itemView.findViewById(R.id.name);
            artist= (TextView) itemView.findViewById(R.id.artist);
        }
    }
    /**
     * 对外提供more点击事件的接口
     *
     */
    public interface OnMoreClickListener{
        void onMoreClickListener(Song song);
    }
}
